package com.cedarhd.brzx.domain.service;

import com.cedarhd.brzx.common.utils.yamlUtil;
import ime.service.client.SOAClient;
import ime.service.util.RSAUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Map;


/**
 * @author Evan
 * @date 2018/4/26
 */
public class SoaBaseService {

    /**
     * 证书名称
     */
    protected static String alias;

    /**
     * 证书文件路径
     */
    protected static String certificatePath;

    /**
     * 密码
     */
    protected static String password;

    /**
     * 商户号
     */
    protected static String sysid;

    /**
     * 支付平台地址
     */
    protected static String serverAddress;

    /**
     * 加密方式
     */
    protected static String  signMethod;

    /**
     * 商户集编号
     */
    protected static String  accountSetNo;


    private static PublicKey publicKey;

    private static PrivateKey privateKey;

    public static SOAClient client;

    protected static RSAUtil rsaUtil;




    private static Logger log = LoggerFactory.getLogger(SoaBaseService.class);

    static {
        log.info("SoaService init!");
        try {
            //init parms
            Map<String, Object> ymlMap = yamlUtil.yaml2Map("application.yml");
            Map<String,Object> tonglianYml = (Map<String, Object>) ymlMap.get("tonglian");
            alias = tonglianYml.get("alias").toString();
            certificatePath = tonglianYml.get("path").toString();
            password = tonglianYml.get("pwd").toString();
            sysid = tonglianYml.get("sysid").toString();
            serverAddress = tonglianYml.get("soalocation").toString();
            signMethod = tonglianYml.get("signMethod").toString();

            accountSetNo = tonglianYml.get("accountSetNo").toString();



            //init client
            privateKey = RSAUtil.loadPrivateKey(alias, certificatePath, password);
            publicKey = RSAUtil.loadPublicKey(alias, certificatePath, password);

            rsaUtil = new RSAUtil((RSAPublicKey)publicKey, (RSAPrivateKey) privateKey);

            client = new SOAClient();
            client.setServerAddress(serverAddress);
            client.setSignKey(privateKey);
            client.setPublicKey(publicKey);
            client.setSysId(sysid);
            client.setSignMethod(signMethod);
            log.info("SoaService init success!");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("SoaService init error !",e);
        }
    }

    /**
     * RSA加密
     * @param str
     * @return
     * @throws Exception
     */
    public static String rsaEncrypt(String str) throws Exception{
        try{
            log.info("rsaEncrypt start");

            log.info("publicKey = {}, privateKey={}", publicKey, privateKey);
            String encryptStr = rsaUtil.encrypt(str);
            return encryptStr;
        }catch(Exception e){
            log.error("rsaEncrypt error, e={}", e);
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * RSA解密
     * @return
     * @throws Exception
     */
    public static String rsaDecrypt(String signStr) throws Exception{
        try{
            log.info("rsaDecrypt start");
            String decryptStr = rsaUtil.dencrypt(signStr);
            log.info("解密：" + decryptStr);
            return  decryptStr;
        }catch(Exception e){
            log.error("rsaDecrypt error, e={}", e);
            e.printStackTrace();
            throw e;
        }
    }
}
